package tote.services;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import tote.security.AppDataSource;

/**
 * Servlet implementation class ImageServlet
 */
public class ImageServlet extends HttpServlet {
    protected final Log log = LogFactory.getLog(ImageServlet.class);
    private static final long serialVersionUID = 1L;

    @Autowired
    AppDataSource dataSource;

    @Override
    public void init(ServletConfig config) throws ServletException {
	SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
	super.init(config);
    }

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
	super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// Enumeration en = request.getSession().getServletContext().getInitParameterNames();
	// while (en.hasMoreElements()) {
	// Object o = en.nextElement();
	// log.debug("CONTEXT PARAM NAME: " + o);
	// }
	Connection conn = null;
	PreparedStatement stmt = null;
	ServletOutputStream out = response.getOutputStream();
	int teamid = 0;

	String steamid = (String) request.getParameter("teamid");
	try {
	    teamid = Integer.parseInt(steamid);
	} catch (Exception e) {
	    // response.setContentType("text/html");
	    // out.println("<html><head><title>Person Photo</title></head>");
	    // out.println("<body><h1>param=" + steamid + "</h1></body></html>");
	    return;
	}

	try {
	    conn = dataSource.getConnection(); // getOracleConnection();
	} catch (Exception e) {
	    response.setContentType("text/html");
	    out.println("<html><head><title>Team Image Servlet</title></head>");
	    out.println("<body><h1>Database Connection Problem.</h1><h2>" + e + "</h2></body></html>");
	    return;
	}

	try {
	    stmt = conn.prepareStatement("select teamlogo from tteam where teamid=?");
	    stmt.setInt(1, teamid);
	    ResultSet rs = stmt.executeQuery();
	    if (rs.next()) {
		response.setContentType("image/gif");
		response.setHeader("Content-type", "image/gif");
		// response.setHeader("Cache-control", "no-cache");
		// response.setHeader("Cache-Control", "no-cache, must-revalidate");
		// response.setHeader("Pragma", "no-cache");
		oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
		if (blob != null) {
		    InputStream is = blob.binaryStreamValue();
		    byte[] buf = new byte[blob.getChunkSize()];
		    int actual = 0;
		    while (((actual = is.read(buf)) != -1))
			out.write(buf, 0, actual);
		    is.close();
		    out.flush();
		}
	    }
	} catch (SQLException e) {
	    response.setContentType("text/html");
	    out.println("<html><head><title>Image getting Error</title></head>");
	    out.println("<body><h1>Error=" + e + "</h1></body></html>");
	    return;
	} finally {
	    try {
		if (stmt != null)
		    stmt.close();
		if (conn != null)
		    conn.close();
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	}
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
	    IOException {

    }

}
